//
//  ViewController.m
//  22-QMusicMVHomework
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "RootModel.h"
#import "Data.h"
#import "Contents.h"
#import "List.h"
#import "MvList.h"
#import "CusCollectionReusableView.h"
#import "CusCollectionViewCell.h"
#define kCollectionViewReuse @"reuse"
#define kCollectionViewHeader @"headReuse"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mvCollectionView;
@property (nonatomic, strong) RootModel *rootModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    [super viewDidLoad];
    [self creatCollectionView];
    [self handleData];
}

- (void)creatCollectionView {
    CGFloat cellMargin = 5;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - cellMargin * 3) / 2, 150);
    flowLayout.minimumLineSpacing = cellMargin;
    flowLayout.minimumInteritemSpacing = cellMargin;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, cellMargin, cellMargin, cellMargin);
    flowLayout.headerReferenceSize = CGSizeMake(0, CGRectGetHeight(self.view.bounds) * 0.4);
    self.mvCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _mvCollectionView.delegate = self;
    _mvCollectionView.dataSource = self;
    _mvCollectionView.backgroundColor = [UIColor whiteColor];
    [_mvCollectionView registerNib:[UINib nibWithNibName:@"CusCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewReuse];
    [_mvCollectionView registerNib:[UINib nibWithNibName:@"CusCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader];
    [self.view addSubview:_mvCollectionView];
    
}

- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"application/x-javascript"];
    manager.responseSerializer.acceptableContentTypes = set;
    [manager GET:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        dataStr = [dataStr stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
        dataStr = [dataStr substringToIndex:dataStr.length - 1];
        NSData *newData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingMutableContainers error:&error];
        self.rootModel = [RootModel modelWithDic:dic];
        [_mvCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error%@", error);
        
    }];
}
#pragma mark - CollectionView DataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CusCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader forIndexPath:indexPath];
    headView.list = _rootModel.data.contents.list[indexPath.section];
    return headView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_rootModel.data.contents.list[section] mvlist].count - 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _rootModel.data.contents.list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CusCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.mvList = [_rootModel.data.contents.list[indexPath.section] mvlist][indexPath.row + 1];
    return cell;
}

#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
